﻿/* SharePoint Console Project
 * Http://SharePointConsole.codeplex.com
 * Copyright Jean-Alain Baeyens (2009)
 * Software under Microsoft Reciprocal License (Ms-RL)
 */

using System;

using Microsoft.SharePoint;
using Microsoft.Office.Server.Search.Administration;
using Sharepoint.Console.Commands;

namespace Sharepoint.Console.Search
{
    class CreateManagedProperty : BaseCommande
    {
        public override string ShortHelp
        {
            get
            {
                return "Create a new managed property";
            }
        }

        public override string LongHelp
        {
            get
            {
                return "Add a new managed property that can be map crawled properties and used by search engine";
            }
        }


        public override string Syntax
        {
            get
            {
                return "AddManagedProperty [PropertyName] [PropertyType]";
            }
        }

        public override string Execute(string[] args)
        {
            if (args.Length != 3) return string.Format("Invalide syntax. Use {0}", Syntax);

            try
            {
                string propertyType = args[2];
                string propertyName = args[1];

                Schema spSchema = new Schema(SearchContext.GetContext(this.SharepointConnection.Site));
                ManagedPropertyCollection properties = spSchema.AllManagedProperties;

                switch (propertyType.ToLower())
                {
                    case "datetime":
                        properties.Create(propertyName, ManagedDataType.DateTime);
                        break;

                    case "decimal":
                        properties.Create(propertyName, ManagedDataType.Decimal);
                        break;
                    
                    case "integer":
                        properties.Create(propertyName, ManagedDataType.Integer);
                        break;
                    
                    case "text":
                        properties.Create(propertyName, ManagedDataType.Text);
                        break;
                    
                    case "yesno":
                        properties.Create(propertyName, ManagedDataType.YesNo);
                        break;
                    
                    case "binary":
                        properties.Create(propertyName, ManagedDataType.Binary);
                        break;
                    
                    default:
                        return "Invalide Property type.";
                }
                return "Property created";

            }

            catch (System.Data.SqlClient.SqlException ex)
            {
                if (ex.ToString().Contains("duplicate"))
                {
                    return "The property already exist !";
                }
                else
                {
                    throw ex;
                }
            }
            
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
